<template>
  <div class="points-ranking">
    <div class="ranking-header">
      <h3 class="ranking-title">积分排行榜</h3>
      <el-button type="primary" text @click="refreshData">
        <el-icon><Refresh /></el-icon> 刷新
      </el-button>
    </div>

    <div v-loading="loading">
      <div v-if="ranking.length === 0" class="empty-ranking">
        暂无排行数据
      </div>
      
      <div v-else class="ranking-list">
        <div v-for="(user, index) in ranking" :key="user.id" class="ranking-item">
          <div class="rank-number" :class="{'top-rank': index < 3}">
            <template v-if="index === 0">🥇</template>
            <template v-else-if="index === 1">🥈</template>
            <template v-else-if="index === 2">🥉</template>
            <template v-else>{{ index + 1 }}</template>
          </div>
          
          <div class="user-info">
            <el-avatar :size="40" :src="user.avatar" class="user-avatar">
              {{ user.name.substr(0, 1) }}
            </el-avatar>
            <div class="user-name">{{ user.name }}</div>
          </div>
          
          <div class="user-points">{{ user.point }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Refresh } from '@element-plus/icons-vue'
import request from '../utils/request'

const loading = ref(false)
const ranking = ref([])

onMounted(() => {
  fetchPointsRanking()
})

async function fetchPointsRanking() {
  loading.value = true
  try {
    // 这里应该调用API获取积分排行
    // 由于API文档中没有直接的积分排行API，这里模拟数据
    // 实际开发时应替换为真实API
    await new Promise(resolve => setTimeout(resolve, 500))
    
    // 模拟数据
    ranking.value = [
      { id: 1, name: '张三', point: 520, avatar: '' },
      { id: 2, name: '李四', point: 485, avatar: '' },
      { id: 3, name: '王五', point: 450, avatar: '' },
      { id: 4, name: '赵六', point: 390, avatar: '' },
      { id: 5, name: '钱七', point: 320, avatar: '' },
      { id: 6, name: '孙八', point: 290, avatar: '' },
      { id: 7, name: '周九', point: 240, avatar: '' },
      { id: 8, name: '吴十', point: 210, avatar: '' },
      { id: 9, name: '郑十一', point: 180, avatar: '' },
      { id: 10, name: '王十二', point: 150, avatar: '' },
    ]
  } catch (error: any) {
    console.error('获取积分排行失败:', error)
    ElMessage.error(error.message || '获取积分排行失败')
  } finally {
    loading.value = false
  }
}

function refreshData() {
  fetchPointsRanking()
}
</script>

<style scoped>
.points-ranking {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.ranking-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.ranking-title {
  font-size: 18px;
  font-weight: 500;
  margin: 0;
}

.ranking-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.ranking-item {
  display: flex;
  align-items: center;
  padding: 10px 15px;
  border-radius: 8px;
  background-color: #f9f9f9;
  transition: all 0.3s;
}

.ranking-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.rank-number {
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background-color: #e6e6e6;
  margin-right: 15px;
  font-weight: bold;
}

.top-rank {
  font-size: 1.2rem;
}

.user-info {
  display: flex;
  align-items: center;
  flex-grow: 1;
  gap: 10px;
}

.user-name {
  font-weight: 500;
}

.user-points {
  font-weight: bold;
  font-size: 1.2rem;
  color: #409eff;
}

.empty-ranking {
  text-align: center;
  color: #909399;
  padding: 40px 0;
}
</style> 